<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('部门列表')" />
    <th:block th:include="include :: datetimepicker-css" />
</head>
<body class="gray-bg">
<div class="container-div">

    <div class="row">
        <div class="col-sm-12 search-collapse">
            <form id="dept-form">
                <div class="select-list">
                    <ul>
                        <li>
                            月份：<input id="dimMonth" autocomplete="off"  placeholder="yyyy-MM" type="text">
                        </li>
                        <li>
                            <a class="btn btn-primary btn-rounded btn-sm" onclick="searchData()"><i class="fa fa-search"></i>&nbsp;搜索</a>
                            <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i class="fa fa-refresh"></i>&nbsp;重置</a>
                            <a class="btn btn-info btn-rounded btn-sm" onclick="saveData()"><i class="fa fa-plus"></i>&nbsp;保存</a>
                        </li>
                    </ul>
                </div>
            </form>
        </div>
        <div class="col-sm-12 select-table table-striped" id="bodyDiv" th:utext="${html}">
        </div>
    </div>
</div>
<th:block th:include="include :: footer" />
<th:block th:include="include :: datetimepicker-js" />
<script th:inline="javascript">
    function searchData() {
        $.ajax({
            url: ctx+'base/weather/searchData?month='+$("#dimMonth").val(),
            type: 'get',
            success: function (data) {
                if(data.code==0) {
                    $.modal.alertError("成功");
                } else {
                    $.modal.alertError(data.msg);
                }
            }
        });
    }
    function saveData() {
        var trs = $(".history-table").find("tr");
        var tds;
        var data=[];
        var dayOccur,tempMax,tempMin,weather,gradeWind;
        $.each(trs,function (index,item) {
            debugger;
            tds = $(this).find("td");
            if(tds.length==6) {
                dayOccur = $(tds[0]).text().substring(0,10);
                tempMax = $(tds[1]).text().replace("°","");
                tempMin = $(tds[2]).text().replace("°","");
                weather = $(tds[3]).text();
                gradeWind = $(tds[4]).text().match(/\d+/)[0];
                data.push({"dayOccur":dayOccur,"tempMax":tempMax,"tempMin":tempMin,"weather":weather,"gradeWind":gradeWind});
            }
        });
        $.ajax({
            url: ctx+'base/weather/addBatch',
            type: 'post',
            dataType: 'json',
            contentType: 'application/json;charset=UTF-8',
            data: JSON.stringify(data),
            success: function (result) {
                if (result.code == 0) {
                    $.modal.alertSuccess("保存成功");
                } else {
                    $.modal.alertError("主键重复，请核实原因");
                }
            }
        });
    }
    $(function () {
        var now = new Date();
        $("#dimMonth").datetimepicker({
            format: "yyyy-mm",
            startView: 3,
            minView: 3,
            maxDate: now.getFullYear() + "-" + (now.getMonth()),
            autoclose: true
        });
        $("#dimMonth").val("2024-03");
        searchData();
    });
</script>
</body>
</html>